﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author mislas
 */
Cognitum.Polizas.Endoso.Entity = function(){
    if (LOG.isDebugEnabled()) {
        LOG.debug('[Cognitum.Polizas.Endoso.Entity.ctor] <-');
    }
	Cognitum.Polizas.Endoso.Entity.superclass.constructor.call(this);
}

Ext.extend(Cognitum.Polizas.Endoso.Entity, Karma.Core.Entity, {
    id: 'Endosos.Entity',
    name: 'Endosos',
    editorXType: 'end.editor',
    editorW: 800,
    editorH: 500,
    columns: [
        { Name: 'Id', Property: 'Id', Mostrar: false }/*,
        { Name: 'Folio', Property: 'Folio' },
        { Name: 'Poliza', Property: 'Poliza.Folio' },
        { Name: 'Ramo', Property: 'Poliza.Ramo.Nombre' },
        { Name: 'SubRamo', Property: 'Poliza.SubRamo.Nombre' },
        { Name: 'Tipo Endoso', Property: 'Tipo.Nombre' },
        { Name: 'Tipo Movimiento', Property: 'TipoMovimiento.Descripcion' },
        { Name: 'Estatus', Property: 'EstatusEndoso', Tipo: 'enum', TipoEnum: 'Cognitum.Dominio.Polizas.EstatusEndoso' }*/
    ],
    titleProperty: new Ext.XTemplate('{[values.Tipo.Nombre]} : ', 
		'<tpl if="!Ext.isEmpty(Folio)"><b>Folio : [{Folio}]</b></tpl>', 
		'<tpl if="Ext.isEmpty(Folio)"><b>OT : [{Id}]</b></tpl>'),
    searchlist: {
        grouping: true,
        sortings: ['Folio'],
        views: [{
            Id: 0,
            Nombre: 'Normal',
            Template: null
        }],
        customevents: {
            'newEndoso': true
        },
        getTBarCustomControls: function(){
            return ['->', this.newToolbarButton = new Ext.Button({
                text: 'Nuevo',
				iconCls: 'icon-plus',
                scope: this,
                menu: {
                    items: [{
                        text: 'Autos',
                        scope: this,
                        menu: {
                            items: [{
                                text: 'Individual',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "0", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "0", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "0", "Individual");
                                        },
                                        scope: this
                                    }]
                                }
                            }, {
                                text: 'Pool',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "0", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "0", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "0", "Pool");
                                        },
                                        scope: this
                                    }]
                                }
                            }]
                        }
                    }, {
                        text: 'Vida',
                        scope: this,
                        menu: {
                            items: [{
                                text: 'Individual',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "2", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "2", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "2", "Individual");
                                        },
                                        scope: this
                                    }]
                                }
                            }, {
                                text: 'Pool',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "2", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "2", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "2", "Pool");
                                        },
                                        scope: this
                                    }]
                                }
                            }]
                        }
                    }, {
                        text: 'GMM',
                        scope: this,
                        menu: {
                            items: [{
                                text: 'Individual',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "1", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "1", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "1", "Individual");
                                        },
                                        scope: this
                                    }]
                                }
                            }, {
                                text: 'Pool',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function(){
                                            this.fireEvent("new", "A", "1", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function(){
                                            this.fireEvent("new", "B", "1", "Pool");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function(){
                                            this.fireEvent("new", "D", "1", "Pool");
                                        },
                                        scope: this
                                    }]
                                }
                            }]
                        }
                    }

                    , {
                        text: 'DAÑOS',
                        scope: this,
                        menu: {
                            items: [{
                                text: 'Individual',
                                scope: this,
                                menu: {
                                    items: [{
                                        text: 'Endoso A',
                                        handler: function() {
                                            this.fireEvent("new", "A", "3", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso B',
                                        handler: function() {
                                        this.fireEvent("new", "B", "3", "Individual");
                                        },
                                        scope: this
                                    }, {
                                        text: 'Endoso D',
                                        handler: function() {
                                        this.fireEvent("new", "D", "3", "Individual");
                                        },
                                        scope: this
                                    }]
                                    }
                                }, {
                                    text: 'Pool',
                                    scope: this,
                                    menu: {
                                        items: [{
                                            text: 'Endoso A',
                                            handler: function() {
                                            this.fireEvent("new", "A", "3", "Pool");
                                            },
                                            scope: this
                                        }, {
                                            text: 'Endoso B',
                                            handler: function() {
                                                this.fireEvent("new", "B", "3", "Pool");
                                            },
                                            scope: this
                                        }, {
                                            text: 'Endoso D',
                                            handler: function() {
                                                this.fireEvent("new", "D", "3", "Pool");
                                            },
                                            scope: this
                                        }]
                                    }
                                    }]
                                    }
                                }
                    
                    
                    
                    ]
                }
            }), '|', ' '];
        }
    },
    aggregatelist: {
        getTBarControls: function(){
            return [this.newToolbarButton = new Ext.Button({
                text: 'Nuevo',
				iconCls: 'icon-plus',
                scope: this,
                menu: {
                    items: [{
                        text: 'EndosoA',
                        scope: this,
                        handler: function(){
                            this.entity.NewFromEntity([{
								Nombre: 'A'
							}, this.parentEntity]);
                        }
                    }, {
                        text: 'EndosoB',
                        scope: this,
                        handler: function(){
                            this.entity.NewFromEntity([{
								Nombre: 'B'
							}, this.parentEntity]);
                        }
                    }, {
                        text: 'EndosoD',
                        scope: this,
                        handler: function(){
                            this.entity.NewFromEntity([{
								Nombre: 'D'
							}, this.parentEntity]);
                        }
                    }]
                }
            })];
        },
        
        openRecord: function(id){
            this.entity.onOpen(id);
        }
    },
    link: {
        displayProperty: new Ext.XTemplate('[OT:{Id}] <tpl if="!Ext.isEmpty(Folio)">{Folio}</tpl>'),
		tpl: '[OT:{Id}] <tpl if="!Ext.isEmpty(Folio)">{Folio}</tpl>'
    },
	
    DescartarMethod: 'DoDescartar',
    CancelarEndoso: 'DoCancela',
    
    getNewTitle: function(value){
        return 'Nuevo Endoso ' + value.Tipo.Nombre;
    },
	
	buildXtype: function(endoso) {
        var tipoEndoso = endoso.Tipo.Nombre;
        var subCategoria = endoso.Poliza.SubRamo.Flotilla ? 'Pool' : 'Individual';
        var ramo;
        switch (endoso.Poliza.Ramo.DivisionOperativa) {
            case 0:
                ramo = 'AUTO'
                break;
            case 1:
                ramo = 'GMM';
                break;
            case 2:
                ramo = 'VIDA';
                break;
            case 3:
                ramo = 'DANHOS';
                break;
        }
        
        var xtype = null;
        if (subCategoria === 'Individual' || tipoEndoso === 'B') {
            xtype = this.editorXType;
        }
        else {
            xtype = 'end.' + ramo + '.' + subCategoria + tipoEndoso + '.editor';
            xtype = xtype.toLowerCase();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug('[Endoso.Entity.openWindow] Xtype: ' + xtype);
        }
		return xtype;
	},
	
    onOpen : function(id, listeners, editorProperties) {
		var editorWin = Karma.WinManager.Instance.getIfExist(this, id);
        if (Ext.isEmpty(editorWin)) {
			this.useInvoker(Karma.Conf.GetMethod, id, {
				fn: function(endoso) {
					this.openEndoso(endoso, listeners, editorProperties);
				},
				scope: this
			});
        } else {
            editorWin.show();
        }
    },
	
	openEndoso: function(endoso, listeners, editorProperties){
	    var xtype = this.buildXtype(endoso);		
        this.openWindow(endoso, null, { value: endoso, listeners: listeners, isNew: false }, xtype);
	},
    
	onNew: function(tipoEndoso, divisionOperativa, subCategoria){
        if (LOG.isDebugEnabled()) {
            LOG.debug('[Endoso.Entity.newEndoso] <-');
        }
        var xtype = null;
        if (subCategoria === 'Individual') {
            xtype = this.editorXType;
        }
        else {
			var ramo = '';
	        switch (divisionOperativa) {
	            case '0':
	                ramo = 'AUTO'
	                break;
	            case '1':
	                ramo = 'GMM';
	                break;
	            case '2':
	                ramo = 'VIDA';
	                break;
	            case '3':
	                ramo = 'DANHOS';
	                break;
	        }
            xtype = 'end.' + ramo + '.' + subCategoria + tipoEndoso + '.editor';
            xtype = xtype.toLowerCase();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug('[Endoso.Entity.newEndoso] Xtype: ' + xtype);
        }
        this.useInvoker(Karma.Conf.NewMethod, {
            Nombre: tipoEndoso
        }, {
            fn: function(result){
                this.openWindow(result, null, {
					ramoClave: divisionOperativa,
                    isNew: true
                }, xtype);
            },
            scope: this
        });
        this.fireEvent('new');
    },
	
	NewFromEntity: function(parameters){
		var ramo = parameters[1].Ramo;
		var editorProperties = { isNew: true, ramoClave: ramo.Clave };
		parameters[1] = { Id: parameters[1].Id };
		
        this.useInvoker(Karma.Conf.NewFromEntityMethod, parameters, {
            fn: function(endoso) {
            var xtype = this.buildXtype(endoso);                
				this.openWindow(endoso, null, editorProperties,xtype);
		        this.fireEvent('new');
		        this.fireEvent('activity');
            },
            scope: this
        });
    }    
	
});

Cognitum.Endosos = Cognitum.Polizas.Endoso.Entity;
